//
//  SlowBloodStretch.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-6-21.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
//
//  拉伸的缓动三层血条，anchorPoint是(0, 0.5),适用规则的血条

#import "SlowBloodStretch.h"

@implementation SlowBloodStretch

@synthesize width = _width;
@synthesize height = _height;

+(id) bloodStretchWithBG:(NSString*)bgFrameName 
                    slow:(NSString*)slowFrameName 
                   blood:(NSString*)bloodFrameName
                   width:(float)width
                  isLeft:(BOOL)isLeft
{
    return [[[self alloc] initStretchWithBG:bgFrameName 
                                       slow:slowFrameName 
                                      blood:bloodFrameName 
                                      width:width
                                     isLeft:isLeft] autorelease]; 
}

-(id) initStretchWithBG:(NSString*)bgFrameName 
                   slow:(NSString*)slowFrameName 
                  blood:(NSString*)bloodFrameName 
                  width:(float)width
                 isLeft:(BOOL)isLeft
{
    if( (self=[super init])) 
    {
        //初始化进度
        _progress = 100;
        
        //血槽的背景
        _bgSprite = [CCSprite spriteWithSpriteFrameName:bgFrameName]; 
         _bgSprite.anchorPoint = ccp(0, 0.5);
        [self addChild:_bgSprite];
        self.width =  _bgSprite.contentSize.width;
        self.height =  _bgSprite.contentSize.height;
        
        //缓动血条
        _maxWidth = width;
        _slowSprite = [CCSprite spriteWithSpriteFrameName:slowFrameName];
        _slowSprite.scaleX = _maxWidth;
        _slowSprite.anchorPoint = ccp(0, 0.5);
        [self addChild:_slowSprite];
        
        //血条
        _bloodSprite = [CCSprite spriteWithSpriteFrameName:bloodFrameName];
        _bloodSprite.scaleX = _maxWidth;
        _bloodSprite.anchorPoint = ccp(0, 0.5);
        [self addChild:_bloodSprite];
        
        [self schedule:@selector(secondUpdate) interval:2.0f];
    }
    
    return self;
}


-(void) setProgress:(int)progress duration:(ccTime)duration
{
    if(progress < 0 || progress > 100) return;
    
    //改变血条 
    _bloodSprite.scaleX = progress * _maxWidth /100;
    
    //改变缓动血条
    [_slowSprite runAction:[CCActionTween actionWithDuration:duration key:@"scaleX" from:_progress * _maxWidth/100 to:progress * _maxWidth /100]];
    
    //改变值
     _progress = progress;
}

- (void)secondUpdate
{
    float HP = _progress;
    HP -= 10;
    [self setProgress:HP duration:4.0];
}

- (void) dealloc
{
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
